package ext.tianma.bom.service;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import ext.tianma.bom.util.EnumerationUtil;
import wt.log4j.LogR;

/**
 * 枚举值、合法值取值
 * 
 * @author gjw
 *
 */

@Controller
@RequestMapping({ "/MaterialAttService" })
public class MaterialAttService {
	@Autowired
	HttpServletRequest request;

	private static final Logger log = LogR.getLogger(MaterialAttService.class.getName());

	// 成品 cn.tianma.FinishedGood
	private static String softype_FinishedGood = "cn.tianma.FinishedGood";

	// 半成品 cn.tianma.SemiFinishedGood
	private static String softype_SemiFinishedGood = "cn.tianma.SemiFinishedGood";

	// cn.tianma.RawMaterial 原材料
	private static String softype_RawMaterial = "cn.tianma.RawMaterial";

	// 原材料 上偏光片 cn.tianma.TopPolarizer
	private static String softype_TopPolarizer = "cn.tianma.TopPolarizer";

	// 原材料 下偏光片 cn.tianma.BotPolarizer
	private static String softype_BotPolarizer = "cn.tianma.BotPolarizer";

	// 原材料 虚拟偏光片组件 cn.tianma.PhantomPolarizer
	private static String softype_PhantomPolarizer = "cn.tianma.PhantomPolarizer";

	// 枚举值
	public static String partNumberEffectiveSite = "料号生效产地";
	public static String finishedGoodDisplayMode = "显示模式";
	public static String polarizerType = "偏光片类型";
	public static String partInspectionStandard = "检验标准";
	public static String partEnvProtectionCategory = "环保类别";
	public static String partSupplier = "供应商名称";

	// 合法值
	public static String panelPlaceOfOrigin = "panelPlaceOfOrigin";// panel产地
	public static String moduleOrigin = "moduleOrigin";// 模组产地

	/**
	 * 获得枚举值料号生效产地
	 * 
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getPartNumberEffectiveSite", method = { RequestMethod.POST, RequestMethod.GET })
	public void getPartNumberEffectiveSite(HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");
		try {
			String result = "";
			JSONObject ob = new JSONObject();
			JSONArray array = new JSONArray();
			Map map = EnumerationUtil.getEnumerationValues(partNumberEffectiveSite);// 获得枚举值料号生效产地
			if (map.size() > 0) {
				for (Object key : map.keySet()) {
					JSONObject ob1 = new JSONObject();
					String key1 = (String) key;
					String value = (String) map.get(key1);
					ob.put("status", "success");
					ob.put("message", "查询成功!");
					ob1.put("researchName", key1);
					ob1.put("showName", value);
					array.put(ob1);
				}
				ob.put("data", array);
			} else {
				ob.put("status", "false");
				ob.put("message", "未查到！");
			}
			result = ob.toString();
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}

	/**
	 * 获得枚举值显示模式
	 * 
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getFinishedGoodDisplayMode", method = { RequestMethod.POST, RequestMethod.GET })
	public void getFinishedGoodDisplayMode(HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");
		try {
			String result = "";
			JSONObject ob = new JSONObject();
			JSONArray array = new JSONArray();
			Map map = EnumerationUtil.getEnumerationValues(finishedGoodDisplayMode);// 获得显示模式
			if (map.size() > 0) {
				for (Object key : map.keySet()) {
					JSONObject ob1 = new JSONObject();
					String key1 = (String) key;
					String value = (String) map.get(key1);
					ob.put("status", "success");
					ob.put("message", "查询成功!");
					ob1.put("researchName", key1);
					ob1.put("showName", value);
					array.put(ob1);
				}
				ob.put("data", array);
			} else {
				ob.put("status", "false");
				ob.put("message", "未查到！");
			}
			result = ob.toString();
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}

	/**
	 * 获得枚举值偏光片类型
	 * 
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getPolarizerType", method = { RequestMethod.POST, RequestMethod.GET })
	public void polarizerType(HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");
		try {
			String result = "";
			JSONObject ob = new JSONObject();
			JSONArray array = new JSONArray();
			Map map = EnumerationUtil.getEnumerationValues(polarizerType);// 获得偏光片类型
			if (map.size() > 0) {
				for (Object key : map.keySet()) {
					JSONObject ob1 = new JSONObject();
					String key1 = (String) key;
					String value = (String) map.get(key1);
					ob.put("status", "success");
					ob.put("message", "查询成功!");
					ob1.put("researchName", key1);
					ob1.put("showName", value);
					array.put(ob1);
				}
				ob.put("data", array);
			} else {
				ob.put("status", "false");
				ob.put("message", "未查到！");
			}
			result = ob.toString();
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}

	/**
	 * 获得枚举值检验标准
	 * 
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getPartInspectionStandard", method = { RequestMethod.POST, RequestMethod.GET })
	public void getPartInspectionStandard(HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");
		try {
			String result = "";
			JSONObject ob = new JSONObject();
			JSONArray array = new JSONArray();
			Map map = EnumerationUtil.getEnumerationValues(partInspectionStandard);// 获得检验标准
			if (map.size() > 0) {
				for (Object key : map.keySet()) {
					JSONObject ob1 = new JSONObject();
					String key1 = (String) key;
					String value = (String) map.get(key1);
					ob.put("status", "success");
					ob.put("message", "查询成功!");
					ob1.put("researchName", key1);
					ob1.put("showName", value);
					array.put(ob1);
				}
				ob.put("data", array);
			} else {
				ob.put("status", "false");
				ob.put("message", "未查到！");
			}
			result = ob.toString();
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}

	/**
	 * 获得枚举值环保类别
	 * 
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getPartEnvProtectionCategory", method = { RequestMethod.POST, RequestMethod.GET })
	public void getPartEnvProtectionCategory(HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");
		try {
			String result = "";
			JSONObject ob = new JSONObject();
			JSONArray array = new JSONArray();
			Map map = EnumerationUtil.getEnumerationValues(partEnvProtectionCategory);// 获得环保类别
			if (map.size() > 0) {
				for (Object key : map.keySet()) {
					JSONObject ob1 = new JSONObject();
					String key1 = (String) key;
					String value = (String) map.get(key1);
					ob.put("status", "success");
					ob.put("message", "查询成功!");
					ob1.put("researchName", key1);
					ob1.put("showName", value);
					array.put(ob1);
				}
				ob.put("data", array);
			} else {
				ob.put("status", "false");
				ob.put("message", "未查到！");
			}
			result = ob.toString();
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}

	/**
	 * 获得枚举值供应商名称
	 * 
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getPartSupplier", method = { RequestMethod.POST, RequestMethod.GET })
	public void getPartSupplier(HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");
		try {
			String result = "";
			JSONObject ob = new JSONObject();
			JSONArray array = new JSONArray();
			Map map = EnumerationUtil.getEnumerationValues(partSupplier);// 获得供应商名称
			if (map.size() > 0) {
				for (Object key : map.keySet()) {
					JSONObject ob1 = new JSONObject();
					String key1 = (String) key;
					String value = (String) map.get(key1);
					ob.put("status", "success");
					ob.put("message", "查询成功!");
					ob1.put("researchName", key1);
					ob1.put("showName", value);
					array.put(ob1);
				}
				ob.put("data", array);
			} else {
				ob.put("status", "false");
				ob.put("message", "未查到！");
			}
			result = ob.toString();
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}

	/**
	 * 获得panel产地
	 * 
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getPanelPlaceOfOrigin", method = { RequestMethod.POST, RequestMethod.GET })
	public void getPanelPlaceOfOrigin(HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");
		try {
			String result = "";
			JSONObject ob = new JSONObject();
			JSONArray array = new JSONArray();
			List<String> list = EnumerationUtil.getTypeLegalValues(softype_FinishedGood, panelPlaceOfOrigin);// 获得panel产地
			if (list.size() > 0) {
				ob.put("status", "success");
				ob.put("message", "查询成功!");
				for (String key : list) {
					JSONObject ob1 = new JSONObject();
					ob1.put("researchName", key);
					ob1.put("showName", key);
					array.put(ob1);
				}
				ob.put("data", array);
			} else {
				ob.put("status", "false");
				ob.put("message", "未查到！");
			}
			result = ob.toString();
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	/**
	 * 获得模组产地
	 * 
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getModuleOrigin", method = { RequestMethod.POST, RequestMethod.GET })
	public void getModuleOrigin(HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");
		try {
			String result = "";
			JSONObject ob = new JSONObject();
			JSONArray array = new JSONArray();
			List<String> list = EnumerationUtil.getTypeLegalValues(softype_FinishedGood, moduleOrigin);// 获得模组产地
			if (list.size() > 0) {
				ob.put("status", "success");
				ob.put("message", "查询成功!");
				for (String key : list) {
					JSONObject ob1 = new JSONObject();
					ob1.put("researchName", key);
					ob1.put("showName", key);
					array.put(ob1);
				}
				ob.put("data", array);
			} else {
				ob.put("status", "false");
				ob.put("message", "未查到！");
			}
			result = ob.toString();
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
}
